package models

import (
	"github.com/astaxie/beego/orm"
	"fmt"
	"time"
)

//活动表
type Activity struct {
	Id            int64
	AcTitle       string
	AcTime        string
	AcPlace       string
	AcObject      string
	AcNumber      string
	AcContent     string
	CreateTime    string
	Status        string
}
//建立模型
func init() {
	orm.RegisterModel(new(Activity))
}

//获取活动列表
func GetActivitylist(page int64, page_size int64, sort string) (activity []orm.Params, count int64) {
	o := orm.NewOrm()
	Activity := new(Activity)
	qs := o.QueryTable(Activity).Filter("Status", "0")
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * page_size
	}
	qs.Limit(page_size, offset).OrderBy(sort).Values(&activity)
	count, _ = qs.Count()
	fmt.Println("activity:", activity)
	fmt.Println("count:", count)
	return activity, count
}

//添加新闻
func AddActivity(g *Activity) (int64, error) {
	o := orm.NewOrm()
	new := new(Activity)
	new.AcTitle= g.AcTitle
	new.AcPlace = g.AcPlace
	new.AcNumber = g.AcNumber
	new.AcObject = g.AcObject
	new.AcTime = g.AcTime
	new.AcContent = g.AcContent
	new.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	new.Status = "0"
	id, err := o.Insert(new)


	fmt.Println("new.AcTitle:", new.AcTitle)
	fmt.Println("new.AcPlace:",new.AcPlace)
	fmt.Println("new.AcNumbere:", new.AcNumber)
	fmt.Println("new.AcObject:", new.AcObject)
	fmt.Println("new.AcTime:", new.AcTime)
	fmt.Println("new.AcContent:", new.AcContent)
	fmt.Println("new.CreateTime:", new.CreateTime)
	fmt.Println("id:", id)
	return id, err
}

//活动加入回收站
func Recycle_Activity_One(Activity_id int64) (int64, error) {
	o := orm.NewOrm()
	new := new(Activity)
	//更改之前必须先指定主键
	new.Id = Activity_id
	//要更新的内容
	new.Status = "1"
	//更新的ORM格式o.Update(news,"Status","Createtime")
	num, err := o.Update(new, "Status")
	return num, err
}

//活动-回收站 列表
func GetActivityRecyclelist(page int64, page_size int64, sort string) (activity []orm.Params, count int64) {
	o := orm.NewOrm()
	new := new(Activity)
	qs := o.QueryTable(new).Filter("Status", "1")
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * page_size
	}
	qs.Limit(page_size, offset).OrderBy(sort).Values(&activity)
	count, _ = qs.Count()
	fmt.Println("activity:", activity)
	fmt.Println("count:", count)
	return activity, count
}

//从回收站还原数据
func Reduction_Activity_One(Activity_id int64) (int64, error) {
	o := orm.NewOrm()
	new := new(Activity)
	//更改之前必须先指定主键
	new.Id = Activity_id
	//要更新的内容
	new.Status = "0"
	//更新的ORM格式o.Update(news,"Status","Createtime")
	num, err := o.Update(new, "Status")
	return num, err
}

//从回收站还原数据
func Delete_Activity_One(Activity_id int64) (int64, error) {
	o := orm.NewOrm()
	new := new(Activity)
	//更改之前必须先指定主键
	new.Id = Activity_id
	//要更新的内容
	new.Status = "2"
	//更新的ORM格式o.Update(news,"Status","Createtime")
	num, err := o.Update(new, "Status")
	return num, err
}